import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'package:safetymonitor/router/app_pages.dart';
///未完成
class NoCompletedController extends GetxController {
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();
  bool isNotPermissions=true;
  /// 获取列表数据
  Future<List<AlarmRespEntity>> getData(int pageNum, int pageSize, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<AlarmRespEntity>>(Interface.getAlarmPage, queryParameters: {
      'pageNo': pageNum,
      'pageSize': pageSize,
    }).execute();

    List<AlarmRespEntity>? data;
    res.onSuccess((result) {
      data = result.data;
      isNotPermissions=true;
      //过滤7(已完成的)
      data=data?.where((element) => element.status!=7).toList();
      update();
    });
    res.onError((result) {
      if(result.code==403&&result.message=='没有该操作权限'){
        isNotPermissions=false;
        update();
      }
    });
    return data ?? [];
  }


  /// 进入详情页面
  void goDetails(AlarmRespEntity entity) {
    //判断app下颜色
    //1.总公司-只有红色报警级别
    //2.分公司-只有有橙色,蓝色,黄色报警级别 ，其中蓝色,黄色报警级别直接处置(不需要指派和验收)
    if (entity.alarmLevel == 1) {
     //红色报警级别-总 （先指派）
      Get.toNamed(Routes.redDisposePage, arguments: entity)?.then((value) {
        if (value != null) {
          globalKey.currentState?.onRefresh();
        }
      });
    }else if(entity.alarmLevel == 2){
      //橙色报警级别-分 （先指派）
      Get.toNamed(Routes.redDisposePage, arguments: entity)?.then((value) {
        if (value != null) {
          globalKey.currentState?.onRefresh();
        }
      });
    }else{
      //蓝色,黄色报警级别-直接处置
      Get.toNamed(Routes.otherDisposePage, arguments: entity)?.then((value) {
        if (value != null) {
          globalKey.currentState?.onRefresh();
        }
      });
    }
  }


  /// 获取报警状态
  Widget getStatueView(int status) {
    String name = '';
    Color? bgColor;
    switch (status) {
      case 4:
        name = '待处置';
        break;
      case 5:
        name = '处置中';
        bgColor = const Color(0xFFFE9843);
        break;
      case 6:
        name = '待验收';
        bgColor = const Color(0xFFFE9843);
        break;
      case 7:
        name = '已完成';
        bgColor = const Color(0xFF63C320);
        break;
    }

    return SizedBox(
      child: Stack(
        alignment: Alignment.centerRight,
        children: [
          SvgPicture.asset(
            Assets.svgBgStatue,
            height: 12.w,
            width: 12.w,
            fit: BoxFit.cover,
            colorFilter: bgColor == null ? null : ColorFilter.mode(bgColor, BlendMode.srcIn),
          ),
          Text(
            name,
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF3F425B),
            ),
          ),
        ],
      ),
    );
  }
}
